//
//  SCEmoticonKeyboard.swift
//  weibo
//
//  Created by 阿城 on 15/12/15.
//  Copyright © 2015年 阿城. All rights reserved.
//

import UIKit

class SCEmoticonKeyboard: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        //配置底部工具栏
        addSubview(EmoticonBar)
        EmoticonBar.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.bottom.equalTo(self)
            make.height.equalTo(44)
        }
        EmoticonBar.EmoticonBackclosure = {[weak self](type: EmoticonButtonType)->() in
            let idx: NSIndexPath
            switch type{
            case .Resent:
                idx = NSIndexPath(forItem: 0, inSection: 0)
//                printLog("Resent")
            case .Normal:
                idx = NSIndexPath(forItem: 0, inSection: 1)
//                printLog("Normal")
            case .Emoji:
                idx = NSIndexPath(forItem: 0, inSection: 2)
//                printLog("Emoji")
            case .LXH:
                idx = NSIndexPath(forItem: 0, inSection: 3)
//                printLog("LXH")
            }
            self?.EmoticonColletion.selectItemAtIndexPath(idx, animated: false, scrollPosition: UICollectionViewScrollPosition.Left)
//            self?.EmoticonColletion.scrollToItemAtIndexPath(idx, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            self?.setPageControl(idx)
        }
        
        //配置表情框
        addSubview(EmoticonColletion)
        EmoticonColletion.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.bottom.equalTo(EmoticonBar.snp_top)
        }
        EmoticonColletion.emoticonCollectionBackclosure = {[weak self](index: NSIndexPath)->()in
            //改变底部按钮
            self?.EmoticonBar.changeSelectedButton(index.section)
            //改变pageControl
            self?.setPageControl(index)
        }
        
        //添加页标
        addSubview(pageControl)
        pageControl.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.bottom.equalTo(EmoticonBar.snp_top)
        }
        
        //主队列异步  显示默认
        dispatch_async(dispatch_get_main_queue()) { () -> Void in
            
            let index = NSIndexPath(forItem: 0, inSection: 1)
            
            self.EmoticonColletion.selectItemAtIndexPath(index, animated: false, scrollPosition: UICollectionViewScrollPosition.Left)
            self.setPageControl(index)
            self.EmoticonBar.changeSelectedButton(index.section)
        }
        
    }

    private lazy var EmoticonBar: SCEmoticonBar = SCEmoticonBar(frame: CGRectZero)
    private lazy var EmoticonColletion: SCEmoticonCollection = SCEmoticonCollection()
    private lazy var pageControl: UIPageControl = {
        let page = UIPageControl()
        //_pageImage _currentPageImage
        page.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "_pageImage")
        page.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "_currentPageImage")
        return page
    }()
    private func setPageControl(idx: NSIndexPath){
        
        pageControl.hidden = idx.section == 0
        
        pageControl.numberOfPages = SCEmoticonViewModel.shareEmoticons.contentEmoticon[idx.section].count
        pageControl.currentPage = idx.item
    }
}
